package org.pet.controller;

import jakarta.validation.constraints.NotNull;
import org.pet.entity.PetSubVariety;
import org.pet.entity.PetVariety;
import org.pet.service.PetVarietyService;
import org.pet.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/variety")
public class PetVarietyController {

    @Autowired
    PetVarietyService petVarietyService;

    //获取一级目录
    @GetMapping("/list")
    public Result list(){
        return petVarietyService.list();
    }
    //根据获得的一级目录id获取目录下的所有二级目录
    @GetMapping("/subList")
    public Result subList(@RequestParam Integer id){
        return petVarietyService.subList(id);
    }

    @GetMapping("/searchList")
    public Result searchList(@RequestParam String search) {
        return petVarietyService.searchList(search);
    }

    @GetMapping("/searchNotExit")
    public Result searchNotExit() {
        return petVarietyService.searchNotExit();
    }

    //增
    @PostMapping("/add")
    public Result insert(@RequestBody PetVariety petVariety){
        return petVarietyService.insert(petVariety);
    }
    @PostMapping("/subAdd")
    public Result subInsert(@RequestParam @NotNull String variety,
                            @RequestParam @NotNull String category,
                            @RequestParam @NotNull String pic){
        if (variety == null || category == null || variety.isEmpty() ||
                category.isEmpty() || pic.isEmpty() || pic.isEmpty()){
            return Result.error("variety 和 category 不能为空");
        }
        return petVarietyService.subInsert(variety,category,pic);
    }

    //改
    @PutMapping("/update")
    public Result update(@RequestBody PetVariety petVariety){
        return petVarietyService.update(petVariety);
    }
    @PatchMapping("/subUpdate")
    public Result subUpdate(@RequestParam @NotNull String variety,
                            @RequestParam @NotNull String category,
                            @RequestParam @NotNull String pic,
                            @RequestParam @NotNull Integer id){
        return petVarietyService.subUpdate(id,variety,category,pic);
    }

    //删
    @DeleteMapping("/delete")
    public Result delete(@RequestParam Integer id){
        return petVarietyService.delete(id);
    }
    @DeleteMapping("/subDelete")
    public Result subDelete(@RequestParam Integer id){
        return petVarietyService.subDelete(id);
    }
}
